using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Resources;
namespace Microsoft.Phone.Controls
{
	internal static class Transitions
	{
		private static Dictionary<string, string> _storyboardXamlCache;
		private static ITransition GetEnumStoryboard<T>(UIElement element, string name, T mode)
		{
			string name2 = name + Enum.GetName(typeof(T), mode);
			Storyboard storyboard = Transitions.GetStoryboard(name2);
			if (storyboard == null)
			{
				return null;
			}
			Storyboard.SetTarget(storyboard, element);
			return new Transition(element, storyboard);
		}
		private static Storyboard GetStoryboard(string name)
		{
			if (Transitions._storyboardXamlCache == null)
			{
				Transitions._storyboardXamlCache = new Dictionary<string, string>();
			}
			string text = null;
			if (Transitions._storyboardXamlCache.ContainsKey(name))
			{
				text = Transitions._storyboardXamlCache.get_Item(name);
			}
			else
			{
				string text2 = "/Microsoft.Phone.Controls.Toolkit;component/Transitions/Storyboards/" + name + ".xaml";
				Uri uri = new Uri(text2, 2);
				StreamResourceInfo resourceStream = Application.GetResourceStream(uri);
				using (StreamReader streamReader = new StreamReader(resourceStream.get_Stream()))
				{
					text = streamReader.ReadToEnd();
					Transitions._storyboardXamlCache.set_Item(name, text);
				}
			}
			return XamlReader.Load(text) as Storyboard;
		}
		public static ITransition Roll(UIElement element)
		{
			if (element == null)
			{
				throw new ArgumentNullException("element");
			}
			Storyboard storyboard = Transitions.GetStoryboard("Roll");
			Storyboard.SetTarget(storyboard, element);
			PlaneProjection planeProjection = new PlaneProjection();
			planeProjection.set_CenterOfRotationX(0.5);
			planeProjection.set_CenterOfRotationY(0.5);
			element.set_Projection(planeProjection);
			return new Transition(element, storyboard);
		}
		public static ITransition Rotate(UIElement element, RotateTransitionMode rotateTransitionMode)
		{
			if (element == null)
			{
				throw new ArgumentNullException("element");
			}
			if (!Enum.IsDefined(typeof(RotateTransitionMode), rotateTransitionMode))
			{
				throw new ArgumentOutOfRangeException("rotateTransitionMode");
			}
			PlaneProjection planeProjection = new PlaneProjection();
			planeProjection.set_CenterOfRotationX(0.5);
			planeProjection.set_CenterOfRotationY(0.5);
			element.set_Projection(planeProjection);
			return Transitions.GetEnumStoryboard<RotateTransitionMode>(element, "Rotate", rotateTransitionMode);
		}
		public static ITransition Slide(UIElement element, SlideTransitionMode slideTransitionMode)
		{
			if (element == null)
			{
				throw new ArgumentNullException("element");
			}
			if (!Enum.IsDefined(typeof(SlideTransitionMode), slideTransitionMode))
			{
				throw new ArgumentOutOfRangeException("slideTransitionMode");
			}
			element.set_RenderTransform(new TranslateTransform());
			return Transitions.GetEnumStoryboard<SlideTransitionMode>(element, string.Empty, slideTransitionMode);
		}
		public static ITransition Swivel(UIElement element, SwivelTransitionMode swivelTransitionMode)
		{
			if (element == null)
			{
				throw new ArgumentNullException("element");
			}
			if (!Enum.IsDefined(typeof(SwivelTransitionMode), swivelTransitionMode))
			{
				throw new ArgumentOutOfRangeException("swivelTransitionMode");
			}
			element.set_Projection(new PlaneProjection());
			return Transitions.GetEnumStoryboard<SwivelTransitionMode>(element, "Swivel", swivelTransitionMode);
		}
		public static ITransition Turnstile(UIElement element, TurnstileTransitionMode turnstileTransitionMode)
		{
			if (element == null)
			{
				throw new ArgumentNullException("element");
			}
			if (!Enum.IsDefined(typeof(TurnstileTransitionMode), turnstileTransitionMode))
			{
				throw new ArgumentOutOfRangeException("turnstileTransitionMode");
			}
			PlaneProjection planeProjection = new PlaneProjection();
			planeProjection.set_CenterOfRotationX(0.0);
			element.set_Projection(planeProjection);
			return Transitions.GetEnumStoryboard<TurnstileTransitionMode>(element, "Turnstile", turnstileTransitionMode);
		}
	}
}
